package com.dzt.androidframework.rxpermission;

import android.os.Bundle;
import android.view.View;

import com.dzt.androidframework.R;
import com.dzt.androidframework.activity.ActivityTitle;
import com.dzt.androidframework.utils.JLogKit;
import com.dzt.androidframework.utils.JToastKit;
import com.jakewharton.rxbinding.view.RxView;

import org.xutils.view.annotation.Event;

import rx.functions.Action0;
import rx.functions.Action1;

/**
 * 动态申请权限管理
 * 2017.12.13
 */
public class PermissionActivity extends ActivityTitle {

	private RxPermissions rxPermissions;

	@Override
	protected void initData(Bundle savedInstanceState) {

	}

	@Override
	protected String[] initPermissions() {
		return new String[0];
	}

	@Override
	protected int getLayoutId() {
		return R.layout.activity_permission;
	}

	@Override
	protected void initParams() {
		setTitleView("Android6.0权限管理");
		rxPermissions = new RxPermissions(this);
		rxPermissions.setLogging(true);
		//通过条件触发获取权限(结合RxBinding使用)
		RxView.clicks(findViewById(R.id.btn_request_camera))
				.compose(rxPermissions.ensureEach(Permission.Dangerous.CAMERA))
				.subscribe(new Action1<Permission>() {
					@Override
					public void call(Permission permission) {
						if (permission.granted) {
							JToastKit.getInstance().showMessage("成功获取CAMERA权限");
						} else if (permission.shouldShowRequestPermissionRationale) {
							JToastKit.getInstance().showMessage("Denied permission without ask never again");
						} else {
							JToastKit.getInstance().showMessage("Permission denied, can't enable the camera");
						}
					}
				}, new Action1<Throwable>() {
					@Override
					public void call(Throwable throwable) {
						JLogKit.getInstance().e("onError");
					}
				}, new Action0() {
					@Override
					public void call() {
						JLogKit.getInstance().e("onComplete");
					}
				});
	}

	@Event(value = {R.id.btn_request_camera, R.id.btn_request_call_phone,
			R.id.btn_request_read_write})
	private void onViewClick(View v) {
		switch (v.getId()) {
			case R.id.btn_request_camera:
				rxPermissions
						.requestEach(Permission.Dangerous.CAMERA)
						.subscribe(new Action1<Permission>() {
							@Override
							public void call(Permission permission) {
								if (permission.granted) {
									JToastKit.getInstance().showMessage("成功获取CAMERA权限");
								} else if (permission.shouldShowRequestPermissionRationale) {
									JToastKit.getInstance().showMessage("Denied permission without ask never again");
								} else {
									JToastKit.getInstance().showMessage("Permission denied, can't enable the camera");
								}
							}
						}, new Action1<Throwable>() {
							@Override
							public void call(Throwable throwable) {
								JLogKit.getInstance().e("onError");
							}
						}, new Action0() {
							@Override
							public void call() {
								JLogKit.getInstance().e("onComplete");
							}
						});
				break;
			case R.id.btn_request_call_phone:
				rxPermissions
						.requestEach(Permission.Dangerous.CALL_PHONE)
						.subscribe(new Action1<Permission>() {
							@Override
							public void call(Permission permission) {
								if (permission.granted) {
									JToastKit.getInstance().showMessage("成功获取电话权限");
								} else if (permission.shouldShowRequestPermissionRationale) {
									JToastKit.getInstance().showMessage("Denied permission without ask never again");
								} else {
									JToastKit.getInstance().showMessage("Permission denied, can't enable the camera");
								}
							}
						}, new Action1<Throwable>() {
							@Override
							public void call(Throwable throwable) {
								JLogKit.getInstance().e("onError");
							}
						}, new Action0() {
							@Override
							public void call() {
								JLogKit.getInstance().e("onComplete");
							}
						});
				break;
			case R.id.btn_request_read_write:
				rxPermissions
						.requestEach(Permission.Dangerous.READ_EXTERNAL_STORAGE,
								Permission.Dangerous.WRITE_EXTERNAL_STORAGE)
						.subscribe(new Action1<Permission>() {
							@Override
							public void call(Permission permission) {
								if (permission.granted) {
									JToastKit.getInstance().showMessage("成功获取读写权限");
								} else if (permission.shouldShowRequestPermissionRationale) {
									JToastKit.getInstance().showMessage("Denied permission without ask never again");
								} else {
									JToastKit.getInstance().showMessage("Permission denied, can't enable the camera");
								}
							}
						}, new Action1<Throwable>() {
							@Override
							public void call(Throwable throwable) {
								JLogKit.getInstance().e("onError");
							}
						}, new Action0() {
							@Override
							public void call() {
								JLogKit.getInstance().e("onComplete");
							}
						});
				break;
			default:
				break;
		}
	}
}
